package user;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

import com.mysql.jdbc.*;


public class OrderForm extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		String SubmitUpdate = request.getParameter("SubmitUpdate");
		PrintWriter out = response.getWriter();	
		 if (SubmitUpdate != null && SubmitUpdate.length() > 0)
		 {
			DBConnector db = DBConnector.getDBConnector();		
			//String Rest_ID = RestaurantQuery.getRestaurantSelected();
			
			try{
				db.prepareStatement("select max(form_ID) from order_form");
				ResultSet NumOrderForm = db.queryPstmt();
				NumOrderForm.next();
				int iNumOrderForm = NumOrderForm.getInt(1);
				//String sNumOrderForm = NumOrderForm.getString(1);
				int i = 1;
				while (i <= iNumOrderForm)
				{
					String index = "" + i;
					String OrderStatus = request.getParameter(index);
					if (OrderStatus != null)
					{
						String statement = "update order_form set form_state = '" + OrderStatus + "' where form_ID = " + index;
						db.prepareStatement(statement);
						db.updatePstmt();
						
						//if the order_form state is "done" which means the whole order process is complete, 
						//then the food sale quantity will be updated. The restaurant revenue will also be updated
						//the cost of the restaurnant will also be updated
						if (OrderStatus.equals("done"))
						{
							String queryFoods = "select food.food_ID, order_quantity, food_price, food_cost from contain, food where food.food_ID = contain.food_ID and form_ID = " + index;
							db.prepareStatement(queryFoods);
							ResultSet Foods = db.queryPstmt();
							if (Foods != null)
							{
								while (Foods.next())
								{
									String updateFoods = "update food set food_quantity = food_quantity + " + Foods.getString(2) + " where food_ID = " + Foods.getString(1);
									db.prepareStatement(updateFoods);
									db.updatePstmt();
									
									String queryRestaurants = "select restaurant.rest_ID, rest_revenue, rest_cost from restaurant, food where food.rest_ID = restaurant.rest_ID and food.food_ID = " + Foods.getString(1); 
									db.prepareStatement(queryRestaurants);
									ResultSet Restaurants = db.queryPstmt();
									
									if (Restaurants != null)
									{
										Restaurants.next();
										float revenue = Foods.getInt(2) * Foods.getFloat(3) + Restaurants.getFloat(2);
										float cost = Foods.getInt(2) * Foods.getFloat(4) + Restaurants.getFloat(3);								
										String updateRestaurantRevenue = "update restaurant set rest_revenue = " + Float.toString(revenue) + " where rest_ID = " + Restaurants.getString(1);
										db.prepareStatement(updateRestaurantRevenue);
										db.updatePstmt();
										String updateRestaurantCost = "update restaurant set rest_cost = " + Float.toString(cost) + " where rest_ID = " + Restaurants.getString(1);
										db.prepareStatement(updateRestaurantCost);
										db.updatePstmt();
									}													
								}
							}					
						}
					}
					i++;
				}	
			}catch (Exception ex) {
				System.out.println("OrderForm exception");
			}
			out.println("<script>alert(unescape('The status update is successful.'))</script>");
			out.println("<script type =\"text/javascript\"> window.location.href = '../administrator_account.jsp'</script>");
			//response.sendRedirect("../administrator_account.jsp");		
		}		
	}
}
